﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElCharro01.Models;

namespace ElCharro01.Areas.User.Controllers
{ 
    public class ProductController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /User/Product/

        public ViewResult Index()
        {
            return View(db.ProductSet.ToList());
        }

        public ActionResult CategorySlideShow(int categoryID)
        {

            return View(db.ProductSet.Where(p => p.Category.Id == categoryID && p.InSlideShow).OrderBy(p => p.Nome).ToList());
        }

        //visualizza i prodotti scelti grazie ai menu del clothing type e della categoria
        public ActionResult ShowSelectedProducts(int CategoryId, int ClothingTypeID)
        {

            int CatId;
            int ClothingTypeId;
            //prendo i valori dalla session
            if (CategoryId == 0)
                CatId = (int)Session["categoryId"];
            else
            {
                CatId = CategoryId;
                Session["categoryId"] = CategoryId;
            }
            if (ClothingTypeID == 0)
                ClothingTypeId = (int)Session["clothingTypeId"];
            else
            {
                ClothingTypeId = ClothingTypeID;
                Session["clothingTypeId"] = ClothingTypeID;
            }
            if (CatId != 0 && ClothingTypeId != 0)
            {
                ViewBag.CategoryName = db.CategorySet.Single(c => c.Id == CatId).Nome;
                ViewBag.ClothingTypeName = db.ClothingTypeSet.Single(c => c.ID == ClothingTypeId).Nome;
                return View(db.ProductSet.Include("ClothingType").Include("Category").Where(p => p.ClothingType.ID == ClothingTypeId && p.Category.Id == CatId).ToList());
            }
            else
                if (CatId == 0 && ClothingTypeId != 0)
                {
                    ViewBag.CategoryName = "";
                    ViewBag.ClothingTypeName = db.ClothingTypeSet.Single(c => c.ID == ClothingTypeId).Nome;
                    return View(db.ProductSet.Include("ClothingType").Include("Category").Where(p => p.ClothingType.ID == ClothingTypeId).ToList());
                }
            else
                    if (CatId != 0 && ClothingTypeId == 0)
                    {
                        ViewBag.CategoryName = db.CategorySet.Single(c => c.Id == CatId).Nome;
                        ViewBag.ClothingTypeName = "";
                        return RedirectToAction("CategorySlideShow", new { categoryID = CategoryId });
                    }
            else
                    return RedirectToAction("Index", "Product", new { area = "Admin" });
        }

        //
        // GET: /User/Product/Details/5

        public ViewResult Details(int id)
        {
            Product product = db.ProductSet.Single(p => p.ID == id);
            return View(product);
        }

       
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}